package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.dtos.WmPageDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.thread.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.UUID;

@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {
    @Autowired
    FileStorageService fileStorageService;


    /**
     * 获取该用户的素材库
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findList(WmPageDto dto) {
        dto.checkParam();

        Page page = new Page(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<WmMaterial> lqw = Wrappers.<WmMaterial>lambdaQuery().eq(WmMaterial::getUserId, WmThreadLocalUtil.getUser().getId());

        if(dto.getIsCollection() != null && dto.getIsCollection() == 1) {
            lqw.eq(WmMaterial::getIsCollection,1);
        }

        lqw.orderByDesc(WmMaterial::getCreatedTime);

        page(page,lqw);
        ResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());
        return result;
    }

    /**
     * 上传图片
     *
     * @param file
     * @return
     */
    @Override
    public ResponseResult uploadPicture(MultipartFile file) {
        String originalFilename = file.getOriginalFilename();
        WmUser user = WmThreadLocalUtil.getUser();
        String las = originalFilename.substring(originalFilename.lastIndexOf("."));
        String replace = UUID.randomUUID().toString().replace("-", "");
        String fileName = replace+las;
        WmMaterial wmMaterial = new WmMaterial();
        try {
            InputStream inputStream = file.getInputStream();
            String str = fileStorageService.uploadImgFile("", fileName, inputStream);


            wmMaterial.setUserId(user.getId());
            wmMaterial.setUrl(str);
            wmMaterial.setType((short)0);
            wmMaterial.setIsCollection((short)0);
            wmMaterial.setCreatedTime(new Date());
            save(wmMaterial);
        } catch (IOException e) {
            e.printStackTrace();
        }


        return ResponseResult.okResult(wmMaterial);
    }
}
